/* Includes ------------------------------------------------------------------*/
#include "NCS8801.h"
#include "iic_current.h"
void Dly_10ns (u32 j);


#define TIME 7/////6 ≈ 400khz    7 ≈ 200khz;default setting = 6。

typedef unsigned int uint;
typedef unsigned          char uchar;

BYTE NCS8801_pucData[2];


void NCS8801_I2CSDASetInput(void); // Set I2C SDA signal as input
void NCS8801_I2CSDASetOutput(void); // Set I2C SDA signal as output

void NCS8801_GPIO_Config(void)
{
	GPIO_InitTypeDef GPIO_Initure;  	
	__HAL_RCC_GPIOG_CLK_ENABLE(); 	            //开启GPI0G时钟
    __HAL_RCC_GPIOE_CLK_ENABLE(); 	            //开启GPI0E时钟
    __HAL_RCC_GPIOB_CLK_ENABLE(); 	            //开启GPI0B时钟
    __HAL_RCC_GPIOD_CLK_ENABLE(); 	            //开启GPI0D时钟
	
    /////default IIC line 0
	GPIO_Initure.Pin = NCS8801PIN_SCL;		
	GPIO_Initure.Speed= GPIO_SPEED_HIGH;     
	GPIO_Initure.Pull =GPIO_NOPULL;
	GPIO_Initure.Mode = GPIO_MODE_OUTPUT_OD;	
	HAL_GPIO_Init(GPIO_NCS8801_SCL,&GPIO_Initure); 	 
	
	GPIO_Initure.Pin = NCS8801PIN_SDA;		
	GPIO_Initure.Speed= GPIO_SPEED_HIGH;     
	GPIO_Initure.Pull =GPIO_NOPULL;
	GPIO_Initure.Mode = GPIO_MODE_OUTPUT_OD;	
	HAL_GPIO_Init(GPIO_NCS8801_SDA,&GPIO_Initure); 	
	
//	GPIO_Initure.Pin = GPIO_PIN_6;		
//	GPIO_Initure.Speed= GPIO_SPEED_LOW;     
//	GPIO_Initure.Pull =GPIO_PULLDOWN;
//	GPIO_Initure.Mode = GPIO_MODE_OUTPUT_PP;	
//	HAL_GPIO_Init(GPIOG,&GPIO_Initure); 

/////default IIC line 1
    GPIO_Initure.Pin = FPCIIC_SCL;		
	GPIO_Initure.Speed= GPIO_SPEED_HIGH;     
	GPIO_Initure.Pull =GPIO_NOPULL;
	GPIO_Initure.Mode = GPIO_MODE_OUTPUT_OD;	
	HAL_GPIO_Init(GPIO_FPCIIC_SCL,&GPIO_Initure); 	 
	
	GPIO_Initure.Pin = FPCIIC_SDA;		
	GPIO_Initure.Speed= GPIO_SPEED_HIGH;     
	GPIO_Initure.Pull =GPIO_NOPULL;
	GPIO_Initure.Mode = GPIO_MODE_OUTPUT_OD;	
	HAL_GPIO_Init(GPIO_FPCIIC_SDA,&GPIO_Initure); 	
    
/////default IIC line 2
    GPIO_Initure.Pin = eDP_IIC_PIN_SCL;		
	GPIO_Initure.Speed= GPIO_SPEED_HIGH;     
	GPIO_Initure.Pull =GPIO_PULLUP;
	GPIO_Initure.Mode = GPIO_MODE_OUTPUT_OD;	
	HAL_GPIO_Init(GPIO_eDP_IIC__SCL,&GPIO_Initure); 	 
	
	GPIO_Initure.Pin = eDP_IIC_PIN_SDA;		
	GPIO_Initure.Speed= GPIO_SPEED_HIGH;     
	GPIO_Initure.Pull =GPIO_PULLUP;
	GPIO_Initure.Mode = GPIO_MODE_OUTPUT_OD;	
	HAL_GPIO_Init(GPIO_eDP_IIC__SDA,&GPIO_Initure); 	
}

void NCS8801_I2CSDASetInput(void)
{
	GPIO_InitTypeDef GPIO_Initure; 	
	GPIO_Initure.Pin = NCS8801PIN_SDA;		
	GPIO_Initure.Speed= GPIO_SPEED_HIGH;     
	GPIO_Initure.Pull =GPIO_NOPULL;
	GPIO_Initure.Mode = GPIO_MODE_INPUT;	
	HAL_GPIO_Init(GPIO_NCS8801_SDA,&GPIO_Initure); 
} 
void NCS8801_I2CSDASetOutput(void)
{ 
	GPIO_InitTypeDef GPIO_Initure; 	
	GPIO_Initure.Pin = NCS8801PIN_SDA;		
	GPIO_Initure.Speed= GPIO_SPEED_HIGH;     
	GPIO_Initure.Pull =	GPIO_NOPULL;//iic总线有上拉电阻，无需拉高GPIO_PULLUP;
	GPIO_Initure.Mode = GPIO_MODE_OUTPUT_OD;//GPIO_MODE_OUTPUT_PP;	
	HAL_GPIO_Init(GPIO_NCS8801_SDA,&GPIO_Initure); 
}
void iic_I2CSDASetOutput(void)
{ 
	GPIO_InitTypeDef GPIO_Initure; 	
	GPIO_Initure.Pin = NCS8801PIN_SDA;		
	GPIO_Initure.Speed= GPIO_SPEED_HIGH;     
	GPIO_Initure.Pull =	GPIO_NOPULL;//iic总线有上拉电阻，无需拉高GPIO_PULLUP;
	GPIO_Initure.Mode = GPIO_MODE_OUTPUT_OD;//GPIO_MODE_OUTPUT_PP;	
	HAL_GPIO_Init(GPIO_NCS8801_SDA,&GPIO_Initure); 
}


void I2CDelay(int time111)
{
    delay_us_sys(time111);
}




void eDP_I2CSDASetInput_device(u8 channel)
{
    GPIO_InitTypeDef GPIO_Initure; 	
	switch(channel)
    {
        case 0:
            GPIO_Initure.Pin = NCS8801PIN_SDA;		
            GPIO_Initure.Speed= GPIO_SPEED_HIGH;     
            GPIO_Initure.Pull =GPIO_NOPULL;
            GPIO_Initure.Mode = GPIO_MODE_INPUT;	
            HAL_GPIO_Init(GPIO_NCS8801_SDA,&GPIO_Initure); 
        break;
        
        case 1:
            GPIO_Initure.Pin = FPCIIC_SDA;		
            GPIO_Initure.Speed= GPIO_SPEED_HIGH;     
            GPIO_Initure.Pull =GPIO_NOPULL;
            GPIO_Initure.Mode = GPIO_MODE_INPUT;	
            HAL_GPIO_Init(GPIO_FPCIIC_SDA,&GPIO_Initure); 
        break;
        
        case 2:
            GPIO_Initure.Pin = eDP_IIC_PIN_SDA;		
            GPIO_Initure.Speed= GPIO_SPEED_HIGH;     
            GPIO_Initure.Pull =GPIO_PULLUP;
            GPIO_Initure.Mode = GPIO_MODE_INPUT;	
            HAL_GPIO_Init(GPIO_eDP_IIC__SDA,&GPIO_Initure); 
        break;
        
        default:
            GPIO_Initure.Pin = NCS8801PIN_SDA;		
            GPIO_Initure.Speed= GPIO_SPEED_HIGH;     
            GPIO_Initure.Pull =GPIO_NOPULL;
            GPIO_Initure.Mode = GPIO_MODE_INPUT;	
            HAL_GPIO_Init(GPIO_NCS8801_SDA,&GPIO_Initure); 
            break;
    }
} 
void eDP_I2CSDASetOutput_device(u8 channel)
{ 
	GPIO_InitTypeDef GPIO_Initure; 	
	
    switch(channel)
    {
        case 0:
            GPIO_Initure.Pin = NCS8801PIN_SDA;		
            GPIO_Initure.Speed= GPIO_SPEED_HIGH;     
            GPIO_Initure.Pull =	GPIO_NOPULL;//iic总线有上拉电阻，无需拉高GPIO_PULLUP;
            GPIO_Initure.Mode = GPIO_MODE_OUTPUT_OD;//GPIO_MODE_OUTPUT_PP;	
            HAL_GPIO_Init(GPIO_NCS8801_SDA,&GPIO_Initure); 
        break;
        
        case 1:
            GPIO_Initure.Pin = FPCIIC_SDA;		
            GPIO_Initure.Speed= GPIO_SPEED_HIGH;     
            GPIO_Initure.Pull =	GPIO_NOPULL;//iic总线有上拉电阻，无需拉高GPIO_PULLUP;
            GPIO_Initure.Mode = GPIO_MODE_OUTPUT_OD;//GPIO_MODE_OUTPUT_PP;	
            HAL_GPIO_Init(GPIO_FPCIIC_SDA,&GPIO_Initure); 
        break;
        
        case 2:
            GPIO_Initure.Pin = eDP_IIC_PIN_SDA;		
            GPIO_Initure.Speed= GPIO_SPEED_HIGH;     
            GPIO_Initure.Pull =	GPIO_PULLUP;//iic总线有上拉电阻，无需拉高GPIO_PULLUP;
            GPIO_Initure.Mode = GPIO_MODE_OUTPUT_OD;//GPIO_MODE_OUTPUT_PP;	
            HAL_GPIO_Init(GPIO_eDP_IIC__SDA,&GPIO_Initure); 
        break;
        
        default:
            GPIO_Initure.Pin = NCS8801PIN_SDA;		
            GPIO_Initure.Speed= GPIO_SPEED_HIGH;     
            GPIO_Initure.Pull =	GPIO_NOPULL;//iic总线有上拉电阻，无需拉高GPIO_PULLUP;
            GPIO_Initure.Mode = GPIO_MODE_OUTPUT_OD;//GPIO_MODE_OUTPUT_PP;	
            HAL_GPIO_Init(GPIO_NCS8801_SDA,&GPIO_Initure); 
            break;
    }
}

//void I2CStart(void)
//{
//	NCS8801_I2CSDASetOutput();I2CDelay(TIME);
//    NCS8801_TPSDA_H;
//	I2CDelay(TIME);
//	NCS8801_TPSCL_H;
//	I2CDelay(TIME);
//	NCS8801_TPSDA_L;
//	I2CDelay(TIME);
//	NCS8801_TPSCL_L;
//}

///******************************************/
///**I2CSendByte**/
///******************************************/
//void I2CSendByte(uchar ucWRData)
//{
//	uint i;	
//    NCS8801_I2CSDASetOutput();I2CDelay(TIME);
//	for(i=0;i<8;i++)
//	{
//		NCS8801_TPSCL_L;
//		if((ucWRData >> 7) & 0x01) 
//			NCS8801_TPSDA_H;				//最高位是否为1,为则SDA= 1
//		else
//			NCS8801_TPSDA_L; 				//否则SDA=0
//		I2CDelay(TIME);
//		NCS8801_TPSCL_H;
//		I2CDelay(TIME);
//		NCS8801_TPSCL_L;
//		ucWRData <<= 1;				//数据左移一位,进入下一轮送数
//	}
//	NCS8801_TPSDA_H; I2CDelay(TIME); 
//	NCS8801_TPSCL_L; I2CDelay(TIME);	
//	NCS8801_TPSDA_H; I2CDelay(TIME);						//等待ack
//}





///******************************************/
///**I2CReceiveACK**/
///******************************************/
//void I2CReceiveACK(void)
//{
//	uchar i=0;
//	NCS8801_TPSCL_H;
//	I2CDelay(TIME);
//    NCS8801_I2CSDASetInput();I2CDelay(TIME);
//	if(i<255)
//	{
//		if (HAL_GPIO_ReadPin(GPIO_NCS8801_SDA, NCS8801PIN_SDA)){}
//	}
//    NCS8801_I2CSDASetOutput();I2CDelay(TIME);
//	NCS8801_TPSDA_L;
//	NCS8801_TPSCL_L;
//	I2CDelay(TIME);
//}

///******************************************/
///**I2CStop**/
///******************************************/
//void I2CStop(void)
//{	
//	NCS8801_I2CSDASetOutput();I2CDelay(TIME);
//    NCS8801_TPSDA_L;
//	NCS8801_TPSCL_L;
//	I2CDelay(TIME);
//	NCS8801_TPSCL_H;
//	I2CDelay(TIME);
//	NCS8801_TPSDA_H;
//}
//void I2CSend_Data(uchar dev_addr_w,uchar address,uchar date)
//{
//	I2CDelay(TIME);
//	I2CStart();
//	I2CSendByte(dev_addr_w);
//	I2CReceiveACK();
//	I2CSendByte(address);
//	I2CReceiveACK();
//	I2CSendByte(date);
//	I2CReceiveACK();
//	I2CStop();
//}
///******************************************/
///**I2CReceiveByte**/
///******************************************/
//uchar I2CReceiveByte(void)
//{
//	unsigned char i;
//	unsigned char ucRDData = 0;
//	unsigned char ucDataBit = 0;
//	NCS8801_TPSCL_L;
//	I2CDelay(TIME);
//	NCS8801_TPSDA_H;
//    NCS8801_I2CSDASetInput();I2CDelay(TIME);
//	for(i = 0;i < 8;i++)
//	{
//		NCS8801_TPSCL_H;
//		I2CDelay(TIME);
//        if(HAL_GPIO_ReadPin(GPIO_NCS8801_SDA, NCS8801PIN_SDA))
//		{ucDataBit=1;}
//        else{ucDataBit=0;}
//		I2CDelay(TIME);
//		ucRDData = ((ucRDData << 1) | ucDataBit);
//		NCS8801_TPSCL_L;
//		I2CDelay(TIME);
//	}
//    NCS8801_I2CSDASetOutput();I2CDelay(TIME);
//	return(ucRDData);
//}
//uchar I2CReceive_Data(uchar dev_addr_w,uchar address,uchar dev_addr_r)
//{
//	uchar Recv_Date;
//	//启动送地址
//	I2CStart();
//	I2CSendByte(dev_addr_w); //DEVICE DATA :1010_000_0_WRITE
//	I2CReceiveACK();
//	I2CSendByte(address);
//	I2CReceiveACK();
//	//启动改为读操作
//	I2CStart();
//	I2CSendByte(dev_addr_r);//DEVICE DATA:1010_000_0_READ
//	I2CReceiveACK();
//	Recv_Date = I2CReceiveByte();
//	//结束时没有ACK
//	I2CStop();
//	return Recv_Date;
//}
////// I2C send data fuction
//bool NCS8801_I2CSend(BYTE ucDeviceAddr, BYTE ucIndex, u16 RDACValue)
//{
//    bool bRet = TRUE;
//    I2CSend_Data(ucDeviceAddr,ucIndex,RDACValue);
//    return bRet;
//}
//// I2C receive data function
//bool NCS8801_I2CReceive_A(BYTE ucDeviceAddr, BYTE ucIndex, BYTE* NCS8801_pucData) //(器件地址，buffer类型，被读出数据保存位置)
//{
//    bool bRet = TRUE;
//    NCS8801_pucData[0] = I2CReceive_Data(ucDeviceAddr,ucIndex,ucDeviceAddr+1);
//    return bRet;
//}
////Start bit of I2C waveform
void NCS8801_I2CStart(void)
{
    NCS8801_TPSCL_L; 
    TWait();TWait();TWait();TWait();
    NCS8801_I2CSDASetOutput();
    TWait();
    NCS8801_TPSDA_H; 
    TWait();TWait();TWait();TWait();
    NCS8801_TPSCL_H;
		TWait();TWait();
    NCS8801_TPSDA_L; 
		TWait();TWait();
    NCS8801_TPSCL_L; 
    TWait();
}

// Stop bit of I2C waveform
void NCS8801_I2CStop(void)
{
    NCS8801_I2CSDASetOutput();
    NCS8801_TPSCL_L; 
    TWait();
    NCS8801_TPSDA_L;
    TWait();TWait();
    NCS8801_TPSCL_H;
    TWait();TWait();
    NCS8801_TPSDA_H;
    TWait();
}

// Send one byte from host to client
bool NCS8801_I2CSendByte(BYTE ucData)
{
	bool bRet;
	unsigned char i=8; 
	NCS8801_I2CSDASetOutput();
	do{
		NCS8801_TPSCL_L;
        if (ucData&0x80)
			NCS8801_TPSDA_H;
			else 
			NCS8801_TPSDA_L;
		TWait();//-----------------------????
		NCS8801_TPSCL_H;
		TWait();
		ucData<<=1;
		NCS8801_TPSCL_L;
			//if (i>1) //判断是否传递最后一位数据，若是最后一位则TSCL拉低后不用等待
		TWait();
    }while(--i);
	
    NCS8801_TPSDA_H;TWait();
    NCS8801_TPSCL_L;TWait();
    NCS8801_TPSDA_H;TWait();
    
	NCS8801_I2CSDASetInput();

	bRet=FALSE;
	NCS8801_TPSCL_H;
	TWait();
	if (HAL_GPIO_ReadPin(GPIO_NCS8801_SDA, NCS8801PIN_SDA))
		bRet=TRUE;
	NCS8801_TPSCL_L;
	TWait();
    NCS8801_I2CSDASetOutput();
    NCS8801_TPSDA_L;TWait();
    TWait();
	return bRet;
}
// Send one byte from host to client
bool NCS8801_I2CSend2Byte(BYTE ucData,BYTE ucdATA2)
{
	bool bRet;
	unsigned char i=8; 
	NCS8801_I2CSDASetOutput();
	do{
		NCS8801_TPSCL_L;
        if (ucData&0x80)
			NCS8801_TPSDA_H;
			else 
			NCS8801_TPSDA_L;
		TWait();//-----------------------????
		NCS8801_TPSCL_H;
		TWait();
		ucData<<=1;
		NCS8801_TPSCL_L;
			//if (i>1) //判断是否传递最后一位数据，若是最后一位则TSCL拉低后不用等待
		TWait();
    }while(--i);
    
    i=8;
    do{
		NCS8801_TPSCL_L;
        if (ucdATA2&0x80)
			NCS8801_TPSDA_H;
			else 
			NCS8801_TPSDA_L;
		TWait();//-----------------------????
		NCS8801_TPSCL_H;
		TWait();
		ucdATA2<<=1;
		NCS8801_TPSCL_L;
			//if (i>1) //判断是否传递最后一位数据，若是最后一位则TSCL拉低后不用等待
		TWait();
    }while(--i);
	
//    NCS8801_TPSDA_H;TWait();
    NCS8801_TPSCL_L;TWait();
//    NCS8801_TPSDA_H;TWait();
    
	NCS8801_I2CSDASetInput();

	bRet=FALSE;
	NCS8801_TPSCL_H;
	TWait();
	if (HAL_GPIO_ReadPin(GPIO_NCS8801_SDA, NCS8801PIN_SDA))
		bRet=TRUE;
	NCS8801_TPSCL_L;
	TWait();
    NCS8801_I2CSDASetOutput();
    NCS8801_TPSDA_L;TWait();
    TWait();
	return bRet;
}
  
// Receive one byte form client to host
BYTE NCS8801_I2CReciveByte(bool bAck)
{
	BYTE ucRet,i;
    NCS8801_TPSDA_H;TWait();
    NCS8801_TPSCL_L;TWait();
    NCS8801_TPSDA_H;TWait();
	NCS8801_I2CSDASetInput();TWait();
	ucRet = 0;
	i=8;
	do
	{	NCS8801_TPSCL_H;
		TWait();
		ucRet<<=1;
		if(HAL_GPIO_ReadPin(GPIO_NCS8801_SDA, NCS8801PIN_SDA))
        { 
            ucRet++; 
        }
		NCS8801_TPSCL_L;
                //if (i>1) //判断是否传递最后一位数据，若是最后一位则TSCL拉低后不用等待
		TWait();
	}while(--i);

	NCS8801_I2CSDASetOutput();
    
	NCS8801_TPSCL_H;
    TWait();
	//if (bAck==0)			//这里可能需要调整
    NCS8801_TPSDA_L;
	//else NCS8801_TPSDA_H;
	TWait();
	NCS8801_TPSCL_L;
	TWait();         
	return ucRet;
}

// Receive one byte form client to host
BYTE NCS8801_I2CReciveByte_NO_ack(bool bAck)
{
	BYTE ucRet,i;
    NCS8801_TPSDA_H;TWait();
    NCS8801_TPSCL_L;TWait();
    NCS8801_TPSDA_H;TWait();
	NCS8801_I2CSDASetInput();TWait();
	ucRet = 0;
	i=8;
	do
	{	NCS8801_TPSCL_H;
		TWait();
		ucRet<<=1;
		if(HAL_GPIO_ReadPin(GPIO_NCS8801_SDA, NCS8801PIN_SDA))
        { 
            ucRet++; 
        }
		NCS8801_TPSCL_L;
                //if (i>1) //判断是否传递最后一位数据，若是最后一位则TSCL拉低后不用等待
		TWait();
	}while(--i);

	NCS8801_I2CSDASetOutput();
    
//	NCS8801_TPSCL_H;
//    TWait();
//	//if (bAck==0)			//这里可能需要调整
//    NCS8801_TPSDA_L;
//	//else NCS8801_TPSDA_H;
//	TWait();
//	NCS8801_TPSCL_L;
	TWait();         
	return ucRet;
}

/////////////////////================================================
////-----------------------------------------------------------------
////
////
////
////-----------------------------------------------------------------
/////////////////////================================================


////Start bit of I2C waveform
void NCS8801_I2CStart_device(u8 channel)
{
    eDP_I2CSDASetOutput_device(channel);
    switch(channel)
    {
        case 0:
            NCS8801_TPSCL_L; 
            TWait();TWait();TWait();TWait();
            eDP_I2CSDASetOutput_device(2);
            TWait();
            NCS8801_TPSDA_H; 
            TWait();TWait();TWait();TWait();
            NCS8801_TPSCL_H;
                TWait();TWait();
            NCS8801_TPSDA_L; 
                TWait();TWait();
            NCS8801_TPSCL_L; 
            TWait();
        break;
        
        case 1:
            NCS8801_TPSCL_L; 
            TWait();TWait();TWait();TWait();
            eDP_I2CSDASetOutput_device(2);
            TWait();
            NCS8801_TPSDA_H; 
            TWait();TWait();TWait();TWait();
            NCS8801_TPSCL_H;
                TWait();TWait();
            NCS8801_TPSDA_L; 
                TWait();TWait();
            NCS8801_TPSCL_L; 
            TWait();
       break;
        
        case 2:
            eDP_IIC_TPSCL_L; 
            TWait();TWait();TWait();TWait();
            eDP_I2CSDASetOutput_device(2);
            TWait();
            eDP_IIC_TPSDA_H; 
            TWait();TWait();TWait();TWait();
            eDP_IIC_TPSCL_H;
                TWait();TWait();
            eDP_IIC_TPSDA_L; 
                TWait();TWait();
            eDP_IIC_TPSCL_L; 
            TWait();
       break;
        
        default:
            break;
    }
}

// Stop bit of I2C waveform
void NCS8801_I2CStop_device(u8 channel)
{
    eDP_I2CSDASetOutput_device(channel);
    
    switch(channel)
    {
        case 0:
            NCS8801_TPSCL_L; 
            TWait();
            NCS8801_TPSDA_L;
            TWait();TWait();
            NCS8801_TPSCL_H;
            TWait();TWait();
            NCS8801_TPSDA_H;
            TWait();
        break;
        
        case 1:
            NCS8801_TPSCL_L; 
            TWait();
            NCS8801_TPSDA_L;
            TWait();TWait();
            NCS8801_TPSCL_H;
            TWait();TWait();
            NCS8801_TPSDA_H;
            TWait();
       break;
        
        case 2:
            eDP_IIC_TPSCL_L; 
            TWait();
            eDP_IIC_TPSDA_L;
            TWait();TWait();
            eDP_IIC_TPSCL_H;
            TWait();TWait();
            eDP_IIC_TPSDA_H;
            TWait();
       break;
        
        default:
            break;
    }
}

// Send one byte from host to client
bool NCS8801_I2CSendByte_device(u8 channel,BYTE ucData)
{
	bool bRet;
	unsigned char i=8; 
	eDP_I2CSDASetOutput_device(2);
	do{
		eDP_IIC_TPSCL_L;
        if (ucData&0x80)
			eDP_IIC_TPSDA_H;
			else 
			eDP_IIC_TPSDA_L;
		TWait();//-----------------------????
		eDP_IIC_TPSCL_H;
		TWait();
		ucData<<=1;
		eDP_IIC_TPSCL_L;
			//if (i>1) //判断是否传递最后一位数据，若是最后一位则TSCL拉低后不用等待
		TWait();
    }while(--i);
	
    eDP_IIC_TPSDA_H;TWait();
    eDP_IIC_TPSCL_L;TWait();
    eDP_IIC_TPSDA_H;TWait();
    
	eDP_I2CSDASetInput_device(2);

	bRet=FALSE;
	eDP_IIC_TPSCL_H;
	TWait();
	if (HAL_GPIO_ReadPin(GPIO_eDP_IIC__SCL, eDP_IIC_PIN_SDA))
		bRet=TRUE;
	eDP_IIC_TPSCL_L;
	TWait();
    eDP_I2CSDASetOutput_device(2);
    eDP_IIC_TPSDA_L;TWait();
    TWait();
	return bRet;
}

// Send one byte from host to client
bool NCS8801_I2CSendByte_device_channel(u8 channel,BYTE ucData)
{
	bool bRet;
	unsigned char i=8; 
	eDP_I2CSDASetOutput_device(channel);
	do{
		eDP_IIC_TPSCL_L;
        if (ucData&0x80)
			eDP_IIC_TPSDA_H;
			else 
			eDP_IIC_TPSDA_L;
		TWait();//-----------------------????
		eDP_IIC_TPSCL_H;
		TWait();
		ucData<<=1;
		eDP_IIC_TPSCL_L;
			//if (i>1) //判断是否传递最后一位数据，若是最后一位则TSCL拉低后不用等待
		TWait();
    }while(--i);
	
    eDP_IIC_TPSDA_H;TWait();
    eDP_IIC_TPSCL_L;TWait();
    eDP_IIC_TPSDA_H;TWait();
    
	eDP_I2CSDASetInput_device(channel);

	bRet=FALSE;
	eDP_IIC_TPSCL_H;
	TWait();
	if (HAL_GPIO_ReadPin(GPIO_eDP_IIC__SCL, eDP_IIC_PIN_SDA))
		bRet=TRUE;
	eDP_IIC_TPSCL_L;
	TWait();
    eDP_I2CSDASetOutput_device(channel);
    eDP_IIC_TPSDA_L;//TWait();
    TWait();
	return bRet;
}
// Send one byte from host to client
bool NCS8801_I2CSend2Byte_device(u8 channel,BYTE ucData,BYTE ucdATA2)
{
	bool bRet;
	unsigned char i=8; 
	eDP_I2CSDASetOutput_device(2);
	do{
		eDP_IIC_TPSCL_L;
        if (ucData&0x80)
			eDP_IIC_TPSDA_H;
			else 
			eDP_IIC_TPSDA_L;
		TWait();//-----------------------????
		eDP_IIC_TPSCL_H;
		TWait();
		ucData<<=1;
		eDP_IIC_TPSCL_L;
			//if (i>1) //判断是否传递最后一位数据，若是最后一位则TSCL拉低后不用等待
		TWait();
    }while(--i);
    
    i=8;
    do{
		eDP_IIC_TPSCL_L;
        if (ucdATA2&0x80)
			eDP_IIC_TPSDA_H;
			else 
			eDP_IIC_TPSDA_L;
		TWait();//-----------------------????
		eDP_IIC_TPSCL_H;
		TWait();
		ucdATA2<<=1;
		eDP_IIC_TPSCL_L;
			//if (i>1) //判断是否传递最后一位数据，若是最后一位则TSCL拉低后不用等待
		TWait();
    }while(--i);
	
//    eDP_IIC_TPSDA_H;TWait();
    eDP_IIC_TPSCL_L;TWait();
//    eDP_IIC_TPSDA_H;TWait();
    
	eDP_I2CSDASetInput_device(2);

	bRet=FALSE;
	eDP_IIC_TPSCL_H;
	TWait();
	if (HAL_GPIO_ReadPin(GPIO_eDP_IIC__SDA, eDP_IIC_PIN_SDA))
		bRet=TRUE;
	eDP_IIC_TPSCL_L;
	TWait();
    eDP_I2CSDASetOutput_device(2);
    eDP_IIC_TPSDA_L;TWait();
    TWait();
	return bRet;
}
  
// Receive one byte form client to host
BYTE NCS8801_I2CReciveByte_device(u8 channel,bool bAck)
{
	BYTE ucRet,i;
    eDP_IIC_TPSDA_H;TWait();
    eDP_IIC_TPSCL_L;TWait();
    eDP_IIC_TPSDA_H;TWait();
	eDP_I2CSDASetInput_device(2);TWait();TWait();TWait();
	ucRet = 0;
	i=8;
	do
	{	eDP_IIC_TPSCL_H;
		TWait();TWait();
		ucRet<<=1;
		if(HAL_GPIO_ReadPin(GPIO_eDP_IIC__SDA, eDP_IIC_PIN_SDA))
        { 
            ucRet++; 
        }
		eDP_IIC_TPSCL_L;
                //if (i>1) //判断是否传递最后一位数据，若是最后一位则TSCL拉低后不用等待
		TWait();TWait();
	}while(--i);

	eDP_I2CSDASetOutput_device(2);
    
	eDP_IIC_TPSCL_H;
    TWait();TWait();
	//if (bAck==0)			//这里可能需要调整
    eDP_IIC_TPSDA_L;
	//else eDP_IIC_TPSDA_H;
	TWait();TWait();
	eDP_IIC_TPSCL_L;
	TWait();   TWait();      
	return ucRet;
}
/////////////////////================================================
////-----------------------------------------------------------------
////
////
////
////-----------------------------------------------------------------
/////////////////////================================================
// I2C send data fuction
bool NCS8801_I2CSend(BYTE ucDeviceAddr, BYTE ucIndex, u16 RDACValue)
{
    bool bRet = TRUE;
      NCS8801_I2CStart();

      if(NCS8801_I2CSendByte(ucDeviceAddr & 0xFE) == ACK)
        {
          //ucIndex = (ucIndex<<2) + ((RDACValue>>8)&0x03);

		    if(NCS8801_I2CSendByte(ucIndex) == ACK)
            {
                  if(NCS8801_I2CSendByte(RDACValue&0xff) == NAK)
                  {
                    bRet = FALSE;
                  }
            }
          else
            {
             bRet = FALSE;
            }
        }
      else
      {
      	bRet = FALSE;
      }
      NCS8801_I2CStop();
      return bRet;
}
// I2C send data fuction
bool NCS8801_I2CSend_A(BYTE ucDeviceAddr, BYTE ucIndex, u16 RDACValue)
{
    bool bRet = TRUE;
    BYTE ucDeviceAddr_tcon = 0xC0;
      NCS8801_I2CStart();

      if(NCS8801_I2CSendByte(ucDeviceAddr_tcon & 0xFE) == ACK)
        {
          //ucIndex = (ucIndex<<2) + ((RDACValue>>8)&0x03);

		    if(NCS8801_I2CSendByte(ucDeviceAddr) == ACK)
            {
                if(NCS8801_I2CSendByte(ucIndex) == ACK)
                {
                    if(NCS8801_I2CSendByte(RDACValue&0xff) == NAK)
                      {
                        bRet = FALSE;
                      }
                      else
                      {
                         bRet = FALSE;
                      }
                }
                else
                {
                     bRet = FALSE;
                }
            }
          else
            {
             bRet = FALSE;
            }
        }
      else
      {
      	bRet = FALSE;
      }
      NCS8801_I2CStop();
      return bRet;
}
// I2C send data fuction
bool NCS8801_I2CSend_Array(u8 SigMode ,u8 channel,u8 para[],u8 offerset)
{
    bool bRet = TRUE;
    BYTE ucDeviceAddr_tcon = 0xC0;
    BYTE ucDeviceAddr = 0x00;
    BYTE ucIndex = 0x00;
    BYTE RDACValue = 0x00;
    ucDeviceAddr_tcon = para[1];
    ucDeviceAddr = para[1];
    ucIndex = para[1];
    
      NCS8801_I2CStart();

//      if(NCS8801_I2CSendByte(ucDeviceAddr_tcon & 0xFE) == ACK)
        {
          //ucIndex = (ucIndex<<2) + ((RDACValue>>8)&0x03);
            for(i = 1; i < para[0+offerset]-0; i++)
            {
                ucIndex = para[i+offerset];
                if(NCS8801_I2CSendByte(ucIndex) == ACK)
                {;}
            }
//		    if(NCS8801_I2CSendByte(ucDeviceAddr) == ACK)
//            {
//                if(NCS8801_I2CSendByte(ucIndex) == ACK)
//                {
//                    if(NCS8801_I2CSendByte(RDACValue&0xff) == NAK)
//                      {
//                        bRet = FALSE;
//                      }
//                      else
//                      {
//                         bRet = FALSE;
//                      }
//                }
//                else
//                {
//                     bRet = FALSE;
//                }
//            }
//          else
//            {
//             bRet = FALSE;
//            }
        }
//      else
//      {
//      	bRet = FALSE;
//      }
      NCS8801_I2CStop();
      return bRet;
}

bool NCS8801_I2CSend_Array_gamma(u8 SigMode ,u8 channel,u8 para[],u8 offerset)
{
    bool bRet = TRUE;
    BYTE ucDeviceAddr_tcon = 0xC0;
    BYTE ucDeviceAddr = 0x00;
    BYTE ucIndex = 0x00;
    BYTE RDACValue = 0x00;
    ucDeviceAddr_tcon = para[1];
    ucDeviceAddr = para[1];
    ucIndex = para[1];
    
      NCS8801_I2CStart();


            for(i = 1; i < para[0+offerset]-0; i++)
            {
                ucIndex = para[i+offerset];
                if(NCS8801_I2CSendByte(ucIndex) == ACK)
                {;}
            }

      NCS8801_I2CStop();
      return bRet;
}

// I2C send data fuction
bool NCS8801_I2CSend_Array_IIC_xxBIT(u8 SigMode ,u8 offerset,u8* para)
{
    bool bRet = TRUE;
    BYTE ucDeviceAddr_tcon = 0xC0;
    BYTE ucDeviceAddr = 0x00;
    BYTE ucIndex = 0x00;
    BYTE RDACValue = 0x00;
    ucDeviceAddr_tcon = para[4];
    ucDeviceAddr = para[5];
    ucIndex = para[3];
    
    NCS8801_I2CStart();

    for(i = 1; i <= ucIndex-0; i++)
    {
        ucDeviceAddr = para[i+3];
        if(NCS8801_I2CSendByte(ucDeviceAddr) == ACK)
        {;}
    }

      NCS8801_I2CStop();
      return bRet;
}
// I2C send data fuction
bool NCS8801_I2CSend_Array_IIC_xxBIT_1v8(u8 SigMode ,u8 offerset,u8* para)
{
    bool bRet = TRUE;
    BYTE ucDeviceAddr_tcon = 0xC0;
    BYTE ucDeviceAddr = 0x00;
    BYTE ucIndex = 0x00;
    BYTE RDACValue = 0x00;
    ucDeviceAddr_tcon = para[4];
    ucDeviceAddr = para[5];
    ucIndex = para[3];
    
    NCS8801_I2CStart_device(2);

    for(i = 1; i <= ucIndex-0; i++)
    {
        ucDeviceAddr = para[i+3];
        if(NCS8801_I2CSendByte_device(2,ucDeviceAddr) == ACK)
        {;}
    }

      NCS8801_I2CStop_device(2);
      return bRet;
}

bool IIC_xxBIT_1v8_Array(u8 IIC_Channel,u8 ucDeviceAddr, u8 unDataLength,u8 *buf)
{
    bool bRet = TRUE;
    
    NCS8801_I2CStart_device(IIC_Channel);
    if(NCS8801_I2CSendByte_device_channel(IIC_Channel,ucDeviceAddr) == ACK)
        {;}
    for(int t=0;t<unDataLength;t++)
    {
        NCS8801_I2CSendByte_device_channel(IIC_Channel,buf[t]);		
    }
    NCS8801_I2CStop_device(IIC_Channel);
    return bRet;
}

bool NCS8801_I2CSend_Array_IIC_xxBIT_1v8_Array(BYTE ucDeviceAddr, BYTE ucIndex, BYTE RDACValue)
{
    bool bRet = TRUE;
//    BYTE ucDeviceAddr_tcon = 0xC0;
//    BYTE ucDeviceAddr = 0x00;
//    BYTE ucIndex = 0x00;
//    BYTE RDACValue = 0x00;
//    ucDeviceAddr_tcon = para[4];
//    ucDeviceAddr = para[5];
//    ucIndex = para[3];
    
    NCS8801_I2CStart_device(2);

//    for(i = 1; i <= ucIndex-0; i++)
    {
//        ucDeviceAddr = para[i+3];
        if(NCS8801_I2CSendByte_device(2,ucDeviceAddr) == ACK)
        {;}
        if(NCS8801_I2CSendByte_device(2,ucIndex) == ACK)
        {;}
        if(NCS8801_I2CSendByte_device(2,RDACValue) == ACK)
        {;}
    }

      NCS8801_I2CStop_device(2);
      return bRet;
}
// I2C receive data function
bool NCS8801_I2CReceive(BYTE ucDeviceAddr, BYTE ucIndex, BYTE* NCS8801_pucData, unsigned int unDataLength) //(器件地址，buffer类型，被读出数据保存位置，读出数据长度)
{
    unsigned int i;
    bool bRet = TRUE;
    NCS8801_I2CStart();
    if(NCS8801_I2CSendByte(ucDeviceAddr & 0xFE) == ACK)//FRAME 1 SERIAL BUS ADDRESS BYTE   WRITE 0
        {
			//ucIndex = (ucIndex<<2);
				//	+ NCS8801_pucData[0];
			if(NCS8801_I2CSendByte(ucIndex) == ACK)   //FRAME 2 MOST SIGNIFICANT DATA BYTE
			{
			   //if(NCS8801_I2CSendByte(NCS8801_pucData[0]) == ACK)  //FRAME 3 LEAST SIGNIFICANT DATA BYTE
				{
				//	NCS8801_I2CStop();		 
					NCS8801_I2CStart();
					if(NCS8801_I2CSendByte(ucDeviceAddr | 0x01) == ACK)   //READ 1
					{
						//for(i = 0; i < unDataLength-1; i++)
						{  
                            NCS8801_pucData[i] = NCS8801_I2CReciveByte(ACK);
						}
						//NCS8801_pucData[unDataLength - 1] = NCS8801_I2CReciveByte(NAK);
                        NCS8801_I2CStop();	
					}
					else
					{          bRet = FALSE;           }
				}
//				else
//				{          bRet = FALSE;           }
			}
			else
			{           bRet = FALSE;           }
		}
		else
        {           bRet = FALSE;           }
    NCS8801_I2CStop();
    return bRet;
}
////I2C receive data function
bool NCS8801_I2CReceive_A(BYTE ucDeviceAddr, BYTE ucIndex, BYTE* NCS8801_pucData) //(器件地址，buffer类型，被读出数据保存位置)
{
    
    unsigned int i=0;
    bool bRet = TRUE;
    NCS8801_I2CStart();
    if(NCS8801_I2CSendByte(ucDeviceAddr & 0xFE) == ACK)//FRAME 1 SERIAL BUS ADDRESS BYTE   WRITE 0
        {
			if(NCS8801_I2CSendByte(ucIndex) == ACK)   //FRAME 2 MOST SIGNIFICANT DATA BYTE
			{	 
					NCS8801_I2CStart();
					if(NCS8801_I2CSendByte(ucDeviceAddr | 0x01) == ACK)   //READ 1
					{
                            NCS8801_pucData[0] = NCS8801_I2CReciveByte_NO_ack(ACK);	
					}
					else
					{          
                                //NCS8801_pucData[0] = NCS8801_I2CReciveByte(ACK);
                                bRet = FALSE;           
                    }
			}
			else
			{           bRet = FALSE;           }
		}
		else
        {           bRet = FALSE;           }
    NCS8801_I2CStop();
    return bRet;
}
////I2C receive data function   from TCON
bool NCS8801_I2CReceive_TCON(BYTE ucDeviceAddr, BYTE ucIndex,u8* buffer, BYTE* NCS8801_pucData) //(器件地址，buffer类型，被读出数据保存位置)
{
    
    unsigned int i=0;
    bool bRet = TRUE;
    BYTE ADDRESS_IIC_READ_BEFORE = 0xE0;
    BYTE ADDRESS_IIC_TCON = 0xC0;
    /////before read data ,get setting 
    NCS8801_I2CStart();
//    if(NCS8801_I2CSendByte(ADDRESS_IIC_READ_BEFORE & 0xFE) == ACK)
//    {
//        NCS8801_I2CStart();
//        if(NCS8801_I2CSendByte((ADDRESS_IIC_READ_BEFORE & 0xFE)|0x01) == NAK)
//        {
//            NCS8801_I2CReciveByte(NAK);
//        }
//       else
//        {
//            bRet = FALSE;
//        };
//    }
//    else
//    {
//        bRet = FALSE;
//    }

        for(i = 0; i < ucDeviceAddr-1; i++)
        {
            //ucIndex = buffer[i+3];
            if(NCS8801_I2CSendByte(buffer[i+4]) == ACK)
            {;}
        }  

        ////read data begin   ADDRESS_IIC_TCON
//    if(NCS8801_I2CSendByte(ucDeviceAddr & 0xFE) == ACK)//FRAME 1 SERIAL BUS ADDRESS BYTE   WRITE 0
//        {
//			if(NCS8801_I2CSendByte(ucIndex) == ACK)   //FRAME 2 MOST SIGNIFICANT DATA BYTE
//			{	 
					NCS8801_I2CStart();
					
                    if(NCS8801_I2CSendByte(buffer[4] | 0x01) == ACK)   //READ 1
					{
                        for(i = 0; i < buffer[buffer[3]+3]; i++)
                        {                        
                            NCS8801_pucData[i] = NCS8801_I2CReciveByte(ACK);	
                        }
					}
					else
					{          
                                //NCS8801_pucData[0] = NCS8801_I2CReciveByte(ACK);
                                bRet = FALSE;           
                    }
//			}
//			else
//			{           bRet = FALSE;           }
//		}
//		else
//        {           bRet = FALSE;           }
    NCS8801_I2CStop();
    return bRet;
}

////I2C receive data function   from TCON
bool NCS8801_I2CReceive_TCON_1v8(BYTE ucDeviceAddr, BYTE ucIndex,u8* buffer, BYTE* NCS8801_pucData) //(器件地址，buffer类型，被读出数据保存位置)
{
    
    unsigned int i=0;
    bool bRet = TRUE;
//    BYTE ADDRESS_IIC_READ_BEFORE = 0xE0;
//    BYTE ADDRESS_IIC_TCON = 0xC0;
    /////before read data ,get setting 
    NCS8801_I2CStart_device(2);


        for(i = 0; i < ucDeviceAddr-1; i++)
        {
            if(NCS8801_I2CSendByte_device(2,buffer[i+4]) == ACK)
            {;}
        }   
        NCS8801_I2CStart_device(2);
        
        if(NCS8801_I2CSendByte_device(2,buffer[4] | 0x01) == ACK)   //READ 1
        {
            for(i = 0; i < buffer[buffer[3]+3]; i++)
            {                        
                NCS8801_pucData[i] = NCS8801_I2CReciveByte_device(2,ACK);	
            }
        }
        else
        {          
            bRet = FALSE;           
        }
    NCS8801_I2CStop_device(2);
    return bRet;
}

////I2C receive data function   from TCON
bool I2CReceive_1v8_channel(BYTE ucDevice_channel,BYTE ucDeviceAddr, BYTE ucIndex,u8* buffer, BYTE* NCS8801_pucData) //(器件地址，buffer类型，被读出数据保存位置)
{
    
    unsigned int i=0;
    bool bRet = TRUE;

    NCS8801_I2CStart_device(ucDevice_channel);


        for(i = 0; i < ucDeviceAddr-1; i++)
        {
            if(NCS8801_I2CSendByte_device_channel(ucDevice_channel,buffer[i+4]) == ACK)
            {;}
        }   
        NCS8801_I2CStart_device(ucDevice_channel);
        
        if(NCS8801_I2CSendByte_device_channel(ucDevice_channel,buffer[4] | 0x01) == ACK)   //READ 1
        {
            for(i = 0; i < buffer[buffer[3]+3]; i++)
            {                        
                NCS8801_pucData[i] = NCS8801_I2CSendByte_device_channel(ucDevice_channel,ACK);	
            }
        }
        else
        {          
            bRet = FALSE;           
        }
    NCS8801_I2CStop_device(ucDevice_channel);
    return bRet;
}

void VDD_NCS8801_Init(u16 uvol)
{	
		
	DelayMs(10);
	NCS8801_I2CSend(DEVICE_ADDRESS_VDD, COMMAND_WRITE_CONTROL_INDEX, 3);
	DelayMs(10);	
	NCS8801_I2CSend(DEVICE_ADDRESS_VDD, COMMAND_WRITE_RDAC_INDEX, uvol);	
	DelayMs(10);		
}

void VDDIO_NCS8801_Init(u16 uvol)
{	
	//NCS8801_GPIO_Config();		
	DelayMs(10);
	NCS8801_I2CSend(DEVICE_ADDRESS_VDDIO, COMMAND_WRITE_CONTROL_INDEX, 3);
	DelayMs(10);	
	NCS8801_I2CSend(DEVICE_ADDRESS_VDDIO, COMMAND_WRITE_RDAC_INDEX, uvol);
	DelayMs(10);		
}
void DVDD_NCS8801_Init(u16 uvol)
{
	DelayMs(10);
	NCS8801_I2CSend(DEVICE_ADDRESS_DVDD, COMMAND_WRITE_CONTROL_INDEX, 3);
	DelayMs(10);	
	NCS8801_I2CSend(DEVICE_ADDRESS_DVDD, COMMAND_WRITE_RDAC_INDEX, uvol);
	DelayMs(10);
}

void NCS8801_Init(u16 status)
{
    u8 i=0;
    NCS8801_GPIO_Config();
    if(status == 1)
    {
        SSD_RESET_LOW;////////RST_eDP setting DOWN  =  NCS8801 reset;
        IF_Sel1_Cmd;/////PWD_eDP setting UP    = NCS8801 power  off;
        DelayMs(10);
        IF_Sel1_Video;/////PWD_eDP setting DOWN  = NCS8801 power  on;
        DelayMs(20);
        SSD_RESET_HIGH;///////RST_eDP setting UP  =  NCS8801 start;
				eDP_POWER_HIGH;////////////edp  power board on
    }
    else
    {
        SSD_RESET_LOW;////////RST_eDP setting DOWN  =  NCS8801 reset;
        IF_Sel1_Cmd;/////PWD_eDP setting UP    = NCS8801 power  off;
				eDP_POWER_LOW;////////////edp  power board oFF
        DelayMs(10);
    }
    DelayMs(20);
  
}

void IIC_BIST_Puer_color(u8 R_color,u8 G_color,u8 B_color)
{
///////add TCON IIC bist mode bmp
//    NCS8801_I2CSend_A(0xFF,0x01,0x00);
//    NCS8801_I2CSend_A(0x13,0x78,0x02);
//    NCS8801_I2CSend_A(0x13,0x5B,0x8F);////old use，  bak
    
//    NCS8801_I2CSend_A(0x13,0x40,0x6D);
//    NCS8801_I2CSend_A(0x00,0x05,0x00);
//    NCS8801_I2CSend_A(0x02,0x0D,0x80);
//    NCS8801_I2CSend_A(0x01,0x50,0x00);/// 
//    NCS8801_I2CSend_A(0x01,0x11,0x00);/// 
//    NCS8801_I2CSend_A(0x01,0x12,0x00);/// 
//    NCS8801_I2CSend_A(0x01,0x3A,0x00);/// 
//    NCS8801_I2CSend_A(0x01,0x10,(0x80|((R_color&0xC0)>>2)|((G_color&0xC0)>>4)|((B_color&0xC0)>>6)));
//    NCS8801_I2CSend_A(0x01,0x3C,R_color<<2);
//    NCS8801_I2CSend_A(0x01,0x3D,G_color<<2);
//    NCS8801_I2CSend_A(0x01,0x3E,B_color<<2);
    
    
    ///////add RM69711 IIC bist mode bmp
//    IIC_xxbit(03,80,FE,97
//IIC_xxbit(03,80,33,03
//IIC_xxbit(03,80,FE,97
//IIC_xxbit(03,80,36,80
//IIC_xxbit(03,80,35,00
//IIC_xxbit(03,80,34,00
//IIC_xxbit(03,80,15,FF
//IIC_xxbit(03,80,16,FF
//IIC_xxbit(03,80,17,FF
//    NCS8801_I2CSend_Array_IIC_xxBIT_1v8_Array(0x80,0xFE,0x97);
//    NCS8801_I2CSend_Array_IIC_xxBIT_1v8_Array(0x80,0x33,0x03);
    NCS8801_I2CSend_Array_IIC_xxBIT_1v8_Array(0x80,0xFE,0x97);
    NCS8801_I2CSend_Array_IIC_xxBIT_1v8_Array(0x80,0x36,0x80);
    NCS8801_I2CSend_Array_IIC_xxBIT_1v8_Array(0x80,0x35,0x00);
    NCS8801_I2CSend_Array_IIC_xxBIT_1v8_Array(0x80,0x34,0x00);
    NCS8801_I2CSend_Array_IIC_xxBIT_1v8_Array(0x80,0x15,R_color);
    NCS8801_I2CSend_Array_IIC_xxBIT_1v8_Array(0x80,0x16,G_color);
    NCS8801_I2CSend_Array_IIC_xxBIT_1v8_Array(0x80,0x17,B_color);
}


static int temmp=0;
static int Data_Num_IIC = 0;
static int HalfData_Num = 0;
int SSD2828_W_RAM_buffer_8bits_part_IIC(float pclk,u8 SigMode ,u8* buffer ,u16 length,u8  multiple ,u8 Flag,u8 DisMode)
{
  	u32 i=0,j=0;
    u8 k_rec_first_line=0;
    u16 cycle_num = 2;////1172 = 293*4;    1170= 130*3*3;
    u8 IIC_Channel_bmp = 2;
    
	u32 delay_time=(pclk < 50) ? 50 : 0;	
    Data_Num_IIC = length*multiple*1;
	HalfData_Num=Data_Num_IIC/cycle_num;
    if(SigMode == Video_Mode)
    {
        
        for(j = 0;j<HalfData_Num;j++)
        {        
            NCS8801_I2CStart_device(IIC_Channel_bmp);
            NCS8801_I2CSendByte_device_channel(IIC_Channel_bmp,0x80);
            if((Flag == 1)&&(k_rec_first_line == 0))
            {
                k_rec_first_line = 0xA5;
                NCS8801_I2CSendByte_device_channel(IIC_Channel_bmp,0x2C);
            }
            else
            {
                NCS8801_I2CSendByte_device_channel(IIC_Channel_bmp,0x3C);
            }
            
            for(i = 0; i < cycle_num; i++)
            {
                NCS8801_I2CSendByte_device_channel(IIC_Channel_bmp,buffer[cycle_num*3*j+3*i+2]);
                NCS8801_I2CSendByte_device_channel(IIC_Channel_bmp,buffer[cycle_num*3*j+3*i+1]);
                NCS8801_I2CSendByte_device_channel(IIC_Channel_bmp,buffer[cycle_num*3*j+3*i+0]);
            }
            NCS8801_I2CStop_device(IIC_Channel_bmp);    
        }
        
#ifdef debug_log_output 
printf("\r\n;");
#endif
 
    }
    else if(SigMode == CMD_Mode)
    {
        ;
    }  
    return 0;    
}

void IIC_BIST_LOGIC_NT71181_USE(u8 byte)
{

    switch(byte)
    {
        case 0:///////BIST PATTERN   红色
            NCS8801_I2CSend_A(0x00,0x05,0x00);/// 
            NCS8801_I2CSend_A(0x02,0x0D,0x00);/// 
            NCS8801_I2CSend_A(0x01,0x11,0x0F);
            NCS8801_I2CSend_A(0x01,0x12,0x02);/// 
            NCS8801_I2CSend_A(0x01,0x50,0x10);//
            NCS8801_I2CSend_A(0x01,0x60,0x00);/// 
            NCS8801_I2CSend_A(0x01,0x61,0xFF);/// 
            NCS8801_I2CSend_A(0x01,0x62,0x00);/// 
            NCS8801_I2CSend_A(0x02,0x0D,0x80);/// 
            NCS8801_I2CSend_A(0x01,0x32,0x00);///
            NCS8801_I2CSend_A(0x01,0x3A,0x00);/// 
            NCS8801_I2CSend_A(0x01,0x31,0xFF);/// 
            break;
        
        case 1:////////BIST PATTERN   绿色
            NCS8801_I2CSend_A(0x00,0x05,0x00);/// 
            NCS8801_I2CSend_A(0x02,0x0D,0x00);/// 
            NCS8801_I2CSend_A(0x01,0x11,0x0F);
            NCS8801_I2CSend_A(0x01,0x12,0x02);/// 
            NCS8801_I2CSend_A(0x01,0x50,0x11);//
            NCS8801_I2CSend_A(0x01,0x60,0x00);/// 
            NCS8801_I2CSend_A(0x01,0x61,0xFF);/// 
            NCS8801_I2CSend_A(0x01,0x62,0x00);/// 
            NCS8801_I2CSend_A(0x02,0x0D,0x80);/// 
            NCS8801_I2CSend_A(0x01,0x32,0x00);///
            NCS8801_I2CSend_A(0x01,0x3A,0x00);/// 
            NCS8801_I2CSend_A(0x01,0x31,0xFF);/// 
            break;
        
        case 2:///////BIST PATTERN   蓝色
            NCS8801_I2CSend_A(0x00,0x05,0x00);/// 
            NCS8801_I2CSend_A(0x02,0x0D,0x00);/// 
            NCS8801_I2CSend_A(0x01,0x11,0x0F);
            NCS8801_I2CSend_A(0x01,0x12,0x02);/// 
            NCS8801_I2CSend_A(0x01,0x50,0x12);//
            NCS8801_I2CSend_A(0x01,0x60,0x00);/// 
            NCS8801_I2CSend_A(0x01,0x61,0xFF);/// 
            NCS8801_I2CSend_A(0x01,0x62,0x00);/// 
            NCS8801_I2CSend_A(0x02,0x0D,0x80);/// 
            NCS8801_I2CSend_A(0x01,0x32,0x00);///
            NCS8801_I2CSend_A(0x01,0x3A,0x00);/// 
            NCS8801_I2CSend_A(0x01,0x31,0xFF);/// 
            break;
        
        case 3:///////BIST PATTERN   白色
            NCS8801_I2CSend_A(0x00,0x05,0x00);/// 
            NCS8801_I2CSend_A(0x02,0x0D,0x00);/// 
            NCS8801_I2CSend_A(0x01,0x11,0x0F);
            NCS8801_I2CSend_A(0x01,0x12,0x02);/// 
            NCS8801_I2CSend_A(0x01,0x50,0x14);//
            NCS8801_I2CSend_A(0x01,0x60,0x00);/// 
            NCS8801_I2CSend_A(0x01,0x61,0xFF);/// 
            NCS8801_I2CSend_A(0x01,0x62,0x00);/// 
            NCS8801_I2CSend_A(0x02,0x0D,0x80);/// 
            NCS8801_I2CSend_A(0x01,0x32,0x00);///
            NCS8801_I2CSend_A(0x01,0x3A,0x00);/// 
            NCS8801_I2CSend_A(0x01,0x31,0xFF);/// 
        break;
        
        case 4:///////BIST PATTERN   黑色
            NCS8801_I2CSend_A(0x00,0x05,0x00);/// 
            NCS8801_I2CSend_A(0x02,0x0D,0x00);/// 
            NCS8801_I2CSend_A(0x01,0x11,0x0F);
            NCS8801_I2CSend_A(0x01,0x12,0x02);/// 
            NCS8801_I2CSend_A(0x01,0x50,0x15);//
            NCS8801_I2CSend_A(0x01,0x60,0x00);/// 
            NCS8801_I2CSend_A(0x01,0x61,0xFF);/// 
            NCS8801_I2CSend_A(0x01,0x62,0x00);/// 
            NCS8801_I2CSend_A(0x02,0x0D,0x80);/// 
            NCS8801_I2CSend_A(0x01,0x32,0x00);///
            NCS8801_I2CSend_A(0x01,0x3A,0x00);/// 
            NCS8801_I2CSend_A(0x01,0x31,0xFF);/// 
            break;
        
        case 5:///////BIST PATTERN   gray128
            NCS8801_I2CSend_A(0x00,0x05,0x00);/// 
            NCS8801_I2CSend_A(0x02,0x0D,0x00);/// 
            NCS8801_I2CSend_A(0x01,0x11,0x0F);
            NCS8801_I2CSend_A(0x01,0x12,0x02);/// 
            NCS8801_I2CSend_A(0x01,0x50,0x14);//
            NCS8801_I2CSend_A(0x01,0x60,0x00);/// 
            NCS8801_I2CSend_A(0x01,0x61,0xFF);/// 
            NCS8801_I2CSend_A(0x01,0x62,0x00);/// 
            NCS8801_I2CSend_A(0x02,0x0D,0x80);/// 
            NCS8801_I2CSend_A(0x01,0x32,0x00);///
            NCS8801_I2CSend_A(0x01,0x3A,0x00);/// 
            NCS8801_I2CSend_A(0x01,0x31,0xFF);/// 
            break;
        
        case 6:/// BIST PATTERN 灰阶渐变，左暗右亮
            NCS8801_I2CSend_A(0x00,0x05,0x00);/// 
            NCS8801_I2CSend_A(0x02,0x0D,0x00);/// 
            NCS8801_I2CSend_A(0x01,0x11,0xFF);
            NCS8801_I2CSend_A(0x01,0x12,0xF8);/// 
            NCS8801_I2CSend_A(0x01,0x50,0x00);//
            NCS8801_I2CSend_A(0x01,0x3A,0x01);/// 
            NCS8801_I2CSend_A(0x02,0x0D,0x80);/// 
            NCS8801_I2CSend_A(0x01,0x32,0x00);///
            NCS8801_I2CSend_A(0x01,0x3A,0x00);/// 
            NCS8801_I2CSend_A(0x01,0x31,0xFF);/// 
            break;
        
        case 7:/// BIST PATTERN 灰阶渐变，上暗下亮
            NCS8801_I2CSend_A(0x00,0x05,0x00);/// 
            NCS8801_I2CSend_A(0x02,0x0D,0x00);/// 
            NCS8801_I2CSend_A(0x01,0x11,0xFF);
            NCS8801_I2CSend_A(0x01,0x12,0xF9);/// 
            NCS8801_I2CSend_A(0x01,0x50,0x00);//
            NCS8801_I2CSend_A(0x02,0x0D,0x80);/// 
            NCS8801_I2CSend_A(0x01,0x32,0x00);///
            NCS8801_I2CSend_A(0x01,0x3A,0x00);/// 
            NCS8801_I2CSend_A(0x01,0x31,0xFF);/// 
            break;
        
        case 8:///////BIST PATTERN   Column 
            NCS8801_I2CSend_A(0x00,0x05,0x00);/// 
            NCS8801_I2CSend_A(0x02,0x0D,0x00);/// 
            NCS8801_I2CSend_A(0x01,0x11,0xFF);
            NCS8801_I2CSend_A(0x01,0x12,0xFE);/// 
            NCS8801_I2CSend_A(0x01,0x50,0x00);//
            NCS8801_I2CSend_A(0x01,0x60,0x00);/// 
            NCS8801_I2CSend_A(0x01,0x61,0x00);/// 
            NCS8801_I2CSend_A(0x01,0x62,0x00);/// 
            NCS8801_I2CSend_A(0x02,0x0D,0x80);/// 
            NCS8801_I2CSend_A(0x01,0x32,0x00);///
            NCS8801_I2CSend_A(0x01,0x3A,0x00);/// 
            NCS8801_I2CSend_A(0x01,0x31,0xFF);/// 
            break;
        
        case 9:///////BIST PATTERN  line1
            NCS8801_I2CSend_A(0x00,0x05,0x00);/// 
            NCS8801_I2CSend_A(0x02,0x0D,0x00);/// 
            NCS8801_I2CSend_A(0x01,0x11,0xFF);
            NCS8801_I2CSend_A(0x01,0x12,0xFF);///
            NCS8801_I2CSend_A(0x01,0x50,0x00);/// 
            NCS8801_I2CSend_A(0x01,0x60,0x00);/// 
            NCS8801_I2CSend_A(0x01,0x61,0x00);/// 
            NCS8801_I2CSend_A(0x01,0x62,0x00);/// 
            NCS8801_I2CSend_A(0x02,0x0D,0x80);/// 
            NCS8801_I2CSend_A(0x01,0x32,0x00);///
            NCS8801_I2CSend_A(0x01,0x3A,0x00);/// 
            NCS8801_I2CSend_A(0x01,0x31,0xFF);/// 
            break;
        
        case 10:///////BIST PATTERN  line2
            NCS8801_I2CSend_A(0x00,0x05,0x00);/// 
            NCS8801_I2CSend_A(0x02,0x0D,0x00);/// 
            NCS8801_I2CSend_A(0x01,0x02,0x0F);
            NCS8801_I2CSend_A(0x01,0x03,0xFF);
            NCS8801_I2CSend_A(0x01,0x04,0x0F);
            NCS8801_I2CSend_A(0x01,0x05,0xFF);
            NCS8801_I2CSend_A(0x01,0x06,0x00);
            NCS8801_I2CSend_A(0x01,0x07,0x00);
            NCS8801_I2CSend_A(0x01,0x08,0x00);
            NCS8801_I2CSend_A(0x01,0x09,0x00);
            NCS8801_I2CSend_A(0x01,0x37,0x0C);
            NCS8801_I2CSend_A(0x01,0x38,0xFF);
            NCS8801_I2CSend_A(0x01,0x11,0x00);
            NCS8801_I2CSend_A(0x01,0x12,0x04);/// 
            NCS8801_I2CSend_A(0x01,0x50,0x1B);///
            NCS8801_I2CSend_A(0x01,0x60,0x00);/// 
            NCS8801_I2CSend_A(0x01,0x61,0x00);/// 
            NCS8801_I2CSend_A(0x01,0x62,0x00);/// 
            NCS8801_I2CSend_A(0x02,0x0D,0x80);/// 
            NCS8801_I2CSend_A(0x01,0x32,0x00);///
            NCS8801_I2CSend_A(0x01,0x3A,0x01);/// 
            NCS8801_I2CSend_A(0x01,0x31,0xFF);/// 
            break;
        
        case 11:///////BIST PATTERN  Dot1
            NCS8801_I2CSend_A(0x00,0x05,0x00);/// 
            NCS8801_I2CSend_A(0x02,0x0D,0x00);/// 
            NCS8801_I2CSend_A(0x01,0x02,0x0A);
            NCS8801_I2CSend_A(0x01,0x03,0xAA);
            NCS8801_I2CSend_A(0x01,0x04,0x0A);
            NCS8801_I2CSend_A(0x01,0x05,0xAA);
            NCS8801_I2CSend_A(0x01,0x06,0x05);
            NCS8801_I2CSend_A(0x01,0x07,0x55);
            NCS8801_I2CSend_A(0x01,0x08,0x05);
            NCS8801_I2CSend_A(0x01,0x09,0x55);
            NCS8801_I2CSend_A(0x01,0x37,0x0C);
            NCS8801_I2CSend_A(0x01,0x38,0xFF);
            NCS8801_I2CSend_A(0x01,0x11,0x00);
            NCS8801_I2CSend_A(0x01,0x12,0x04);/// 
            NCS8801_I2CSend_A(0x01,0x50,0x1B);///
            NCS8801_I2CSend_A(0x01,0x60,0x00);/// 
            NCS8801_I2CSend_A(0x01,0x61,0x00);/// 
            NCS8801_I2CSend_A(0x01,0x62,0x00);/// 
            NCS8801_I2CSend_A(0x02,0x0D,0x80);/// 
            NCS8801_I2CSend_A(0x01,0x32,0x00);///
            NCS8801_I2CSend_A(0x01,0x3A,0x01);/// 
            NCS8801_I2CSend_A(0x01,0x31,0xFF);/// 
            break;
        
        case 12:///////BIST PATTERN  RGBW条
            NCS8801_I2CSend_A(0x00,0x05,0x00);/// 
            NCS8801_I2CSend_A(0x02,0x0D,0x00);/// 
            NCS8801_I2CSend_A(0x01,0x11,0x00);
            NCS8801_I2CSend_A(0x01,0x12,0x03);/// 
            NCS8801_I2CSend_A(0x01,0x50,0x00);/// 
            NCS8801_I2CSend_A(0x01,0x60,0x00);/// 
            NCS8801_I2CSend_A(0x01,0x61,0x00);/// 
            NCS8801_I2CSend_A(0x01,0x62,0x00);/// 
            NCS8801_I2CSend_A(0x02,0x0D,0x80);/// 
            NCS8801_I2CSend_A(0x01,0x32,0x00);///
            NCS8801_I2CSend_A(0x01,0x3A,0x01);/// 
            NCS8801_I2CSend_A(0x01,0x31,0xFF);/// 
            break;
        
        case 13:///////BIST PATTERN  对角斜线+边框
            NCS8801_I2CSend_A(0x00,0x05,0x00);/// 
            NCS8801_I2CSend_A(0x02,0x0D,0x00);/// 
            NCS8801_I2CSend_A(0x01,0x11,0x00);
            NCS8801_I2CSend_A(0x01,0x12,0x04);/// 
            NCS8801_I2CSend_A(0x01,0x50,0x00);/// 
            NCS8801_I2CSend_A(0x01,0x60,0x00);/// 
            NCS8801_I2CSend_A(0x01,0x61,0x00);/// 
            NCS8801_I2CSend_A(0x01,0x62,0x00);/// 
            NCS8801_I2CSend_A(0x02,0x0D,0x80);/// 
            NCS8801_I2CSend_A(0x01,0x32,0x00);///
            NCS8801_I2CSend_A(0x01,0x3A,0x01);/// 
            NCS8801_I2CSend_A(0x01,0x31,0xFF);/// 
            break;
        
        case 14:///////BIST PATTERN  中心小白块
            NCS8801_I2CSend_A(0x00,0x05,0x00);/// 
            NCS8801_I2CSend_A(0x02,0x0D,0x00);/// 
            NCS8801_I2CSend_A(0x01,0x11,0x00);
            NCS8801_I2CSend_A(0x01,0x12,0x00);/// 
            NCS8801_I2CSend_A(0x01,0x50,0x1A);/// 
            NCS8801_I2CSend_A(0x01,0x60,0x00);/// 
            NCS8801_I2CSend_A(0x01,0x61,0x00);/// 
            NCS8801_I2CSend_A(0x01,0x62,0x00);/// 
            NCS8801_I2CSend_A(0x02,0x0D,0x80);/// 
            NCS8801_I2CSend_A(0x01,0x32,0x00);///
            NCS8801_I2CSend_A(0x01,0x3A,0x01);/// 
            NCS8801_I2CSend_A(0x01,0x31,0xFF);/// 
            break;
        
        case 15:///////BIST PATTERN  棋盘格
            NCS8801_I2CSend_A(0x00,0x05,0x00);/// 
            NCS8801_I2CSend_A(0x02,0x0D,0x00);/// 
            NCS8801_I2CSend_A(0x01,0x11,0x00);
            NCS8801_I2CSend_A(0x01,0x12,0x0A);/// 
            NCS8801_I2CSend_A(0x01,0x50,0x00);/// 
            NCS8801_I2CSend_A(0x01,0x60,0x00);/// 
            NCS8801_I2CSend_A(0x01,0x61,0x00);/// 
            NCS8801_I2CSend_A(0x01,0x62,0x00);/// 
            NCS8801_I2CSend_A(0x02,0x0D,0x80);/// 
            NCS8801_I2CSend_A(0x01,0x32,0x00);///
            NCS8801_I2CSend_A(0x01,0x3A,0x00);/// 
            NCS8801_I2CSend_A(0x01,0x31,0xFF);/// 
            break;
        
        case 16:///////BIST PATTERN  边框
            NCS8801_I2CSend_A(0x00,0x05,0x00);/// 
            NCS8801_I2CSend_A(0x02,0x0D,0x00);/// 
            NCS8801_I2CSend_A(0x01,0x11,0x00);
            NCS8801_I2CSend_A(0x01,0x12,0x0B);/// 
            NCS8801_I2CSend_A(0x01,0x50,0x00);/// 
            NCS8801_I2CSend_A(0x01,0x60,0x00);/// 
            NCS8801_I2CSend_A(0x01,0x61,0x00);/// 
            NCS8801_I2CSend_A(0x01,0x62,0x00);/// 
            NCS8801_I2CSend_A(0x02,0x0D,0x80);/// 
            NCS8801_I2CSend_A(0x01,0x32,0x00);///
            NCS8801_I2CSend_A(0x01,0x3A,0x00);/// 
            NCS8801_I2CSend_A(0x01,0x31,0xFF);/// 
            break;
        
        case 17:///////BIST PATTERN  RGB白渐变，H方向
            NCS8801_I2CSend_A(0x00,0x05,0x00);/// 
            NCS8801_I2CSend_A(0x02,0x0D,0x00);/// 
            NCS8801_I2CSend_A(0x01,0x11,0x00);
            NCS8801_I2CSend_A(0x01,0x12,0x0C);/// 
            NCS8801_I2CSend_A(0x01,0x50,0x00);/// 
            NCS8801_I2CSend_A(0x01,0x60,0x00);/// 
            NCS8801_I2CSend_A(0x01,0x61,0x00);/// 
            NCS8801_I2CSend_A(0x01,0x62,0x00);/// 
            NCS8801_I2CSend_A(0x02,0x0D,0x80);/// 
            NCS8801_I2CSend_A(0x01,0x32,0x00);///
            NCS8801_I2CSend_A(0x01,0x3A,0x00);/// 
            NCS8801_I2CSend_A(0x01,0x31,0xFF);/// 
            break;
        
        case 18:///////BIST PATTERN  RGB白渐变，V方向
            NCS8801_I2CSend_A(0x00,0x05,0x00);/// 
            NCS8801_I2CSend_A(0x02,0x0D,0x00);/// 
            NCS8801_I2CSend_A(0x01,0x11,0x00);
            NCS8801_I2CSend_A(0x01,0x12,0x0D);/// 
            NCS8801_I2CSend_A(0x01,0x50,0x00);/// 
            NCS8801_I2CSend_A(0x01,0x60,0x00);/// 
            NCS8801_I2CSend_A(0x01,0x61,0x00);/// 
            NCS8801_I2CSend_A(0x01,0x62,0x00);/// 
            NCS8801_I2CSend_A(0x02,0x0D,0x80);/// 
            NCS8801_I2CSend_A(0x01,0x32,0x00);///
            NCS8801_I2CSend_A(0x01,0x3A,0x00);/// 
            NCS8801_I2CSend_A(0x01,0x31,0xFF);/// 
            break;
        
        case 19:///////BIST PATTERN  边框+十字
            NCS8801_I2CSend_A(0x00,0x05,0x00);/// 
            NCS8801_I2CSend_A(0x02,0x0D,0x00);/// 
            NCS8801_I2CSend_A(0x01,0x11,0x00);
            NCS8801_I2CSend_A(0x01,0x12,0x0C);/// 
            NCS8801_I2CSend_A(0x01,0x50,0x00);/// 
            NCS8801_I2CSend_A(0x01,0x60,0x00);/// 
            NCS8801_I2CSend_A(0x01,0x61,0x00);/// 
            NCS8801_I2CSend_A(0x01,0x62,0x00);/// 
            NCS8801_I2CSend_A(0x02,0x0D,0x80);/// 
            NCS8801_I2CSend_A(0x01,0x32,0x00);///
            NCS8801_I2CSend_A(0x01,0x3A,0x01);/// 
            NCS8801_I2CSend_A(0x01,0x31,0xFF);/// 
            break;
        
        case 20:
            
            break;
        
        case 21:
            
            break;
        
        case 22:
            
            break;
        
        
        default://////默认显示64灰阶
            
        break;
    }
}
